package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.relation.RoleResult;
import java.util.List;

/**
 * @Author weizhongli
 * @Date 2020/4/15
 * @Version v1.0
 * @Description 角色业务控制器
 **/
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    //依赖注入
    @Reference
    private RoleService roleService;


    /*
     * 删除角色
     * */
    @RequestMapping("delete")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
            return new Result(true, "删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }

    }


    /*
     * 编辑角色
     * */
    @RequestMapping("edit")
    public Result edit(@RequestBody Role role, Integer[] menuIds, Integer[] PermissionIds) {
        try {
            //调用service中的更新方法
            roleService.edit(role, menuIds, PermissionIds);
            //更新成功, 返回更新成功的信息
            return new Result(true, "编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            //更新失败, 返回更新失败的信息
            return new Result(false, "编辑角色失败");
        }
    }

    /*
     * 根据角色ID获取选中的菜单列表id
     * */
    @RequestMapping("findmenuIdsByRoleId")
    public Result findmenuIdsByRoleId(Integer id) {
        try {
            List<Integer> menuIdsList = roleService.findmenuIdsByRoleId(id);
            return new Result(true, "获取菜单id成功", menuIdsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取菜单id失败");
        }
    }

    /*
     * 根据角色ID获取选中的权限列表id
     * */
    @RequestMapping("findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> PermissionIdsList = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, "获取权限id成功", PermissionIdsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取权限id失败");
        }
    }

    /*
     * 根据ID获取角色数据
     * */
    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /*
     *添加角色
     * */
    @RequestMapping("add")
    public Result add(@RequestBody Role role, Integer[] menuIds, Integer[] PermissionIds) {
        try {
            roleService.add(role, menuIds, PermissionIds);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    /*
     * 查询所有菜单
     * */
    @RequestMapping("findAllMenu")
    public Result findAllMenu() {
        try {
            List<Menu> menuList = roleService.findAllMenu();
            return new Result(true, "查询菜单成功", menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }

    /*
     * 查询所有权限
     * */
    @RequestMapping("findAllPermission")
    public Result findAllPermission() {
        try {
            List<Permission> permissionList = roleService.findAllPermission();
            return new Result(true, "查询权限成功", permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }
    }

    /**
     * 分页查询
     * 1.调用service中的分页查询方法
     * 2.查询成功, 返回查询成功的信息 及 数据
     * 3.查询失败, 返回查询失败的信息 及 数据
     *
     * @param queryPageBean 前端页面发过来的数据( 当前页面, 每页条数, 查询条件)
     * @return 结果(总记录数, 当前页要显示的10条检查组数据)
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("=========================="+queryPageBean);
        try {
            //调用service中的分页查询方法
            PageResult pageResult = roleService.pageQuery(queryPageBean);
            //查询成功, 返回查询成功的信息 及 数据
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败, 返回查询失败的信息 及 数据
            return new PageResult(0L, null);
        }
    }
}
